/*
 * Created by SharpDevelop.
 * User: MyWife
 * Date: 1/1/2003
 * Time: 3:55 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Collections;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices; 
using System.Drawing.Imaging;
using System.Configuration;
using System.Drawing.Drawing2D;
using popLib;
using popLib.ExtensionClass;
using System.Reflection;
using System.IO;

namespace ClockGadget
{
	/// <summary>
	/// Description of Start.
	/// </summary>
	public class Start: SidebarGadget
	{
			
		public static string dir_clock   = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Clocks");
		public enum optionsType { Name, Hour, Minute, Second, TransparentBG }
		
		public Start(SidebarContainer sidebarContainer):base(sidebarContainer){
						
			if( !File.Exists(file_config) || !ini_config.isSectionExist("Setting") ){
				ini_config.setValue( "Setting", "Name", "Clock Gadget" );
				ini_config.setValue( "Setting", "Description", "Various clock in your beautiful dock." );
			}
									
			if( !ini_config.isKeyExist( "Setting", "Options" ) ){
				Hashtable options = new Hashtable();
				options.Add( optionsType.Name.ToString() ,  "Simple" );
				options.Add( optionsType.Hour.ToString() ,  "White" );
				options.Add( optionsType.Minute.ToString(), "White" );
				options.Add( optionsType.Second.ToString(), "Red" );
				options.Add( optionsType.TransparentBG.ToString() , "0" );
				ini_config.setValue( "Setting", "Options", StringLib.deParseOption(options) );
			}
			
			if( !Directory.Exists( dir_clock ) ){				
				Directory.CreateDirectory( dir_clock );
				
				//Create default lock
				ClockRes.Simple.Save( Path.Combine(dir_clock,"Simple.png"), ImageFormat.Png );
				FileLib.writeFile( Path.Combine(dir_clock,"Simple.ini"), "White,White,Red,0" );
			}
			string[] clocks = FileLib.getFileList( dir_clock, "*.png", true, FileLib.SearchOption.FileOnly );
			foreach( string clock in clocks ){
				string name    	= Path.GetFileNameWithoutExtension( clock );
				string config  	= Path.ChangeExtension( clock, "ini" );				
				config			= File.Exists(config) ? FileLib.readFile(config) : "White,White,Red,0";
				
				if( !ini_config.isKeyExist( "Clocks", name ) )
					ini_config.setValue( "Clocks", name, config );
			}
		}
		
		#region Functions
		public override object InitGadget(){
			if( control == null ){
				control = new ClockPanel(this);
				UpdateOptions(false);
			}
			return control;
		}		
						
		public override void ViewOptions(){
			if( sidebarGadgetOption == null ) sidebarGadgetOption = new Options(this);
			sidebarGadgetOption.Show();
		}
		#endregion
		
		
	    
	    #region drawClock
	    static Image result;
		static Graphics painter;
		static GraphicsState transState;
		static DateTime dtNow;
		public static Image drawClock(Image background, Point bgLocation, Color color){
			return drawClock( background, bgLocation , color, color, color );
		}
		
	    public static Image drawClock(Image background, Point bgLocation, Color hourColor, Color minuteColor, Color secondColor){
	    	dtNow = DateTime.Now;
			
			result 		= new Bitmap(130, 130);
			painter 	= Graphics.FromImage( result );
			painter.DrawImage( background, new Rectangle( bgLocation.X, bgLocation.Y, 130, 130 ) );
			
			painter.TranslateTransform( 65, 65 );
			transState 	= painter.Save();
			
           	painter.RotateTransform(dtNow.Minute * 6.0F - 90.0F);
           	painter.SmoothingMode = SmoothingMode.AntiAlias;
           	drawClockHand( minuteColor, new Size(60, 3), 10, true );
           	
           	painter.ResetTransform();
           	painter.TranslateTransform( 65, 65 );
           	painter.RotateTransform(dtNow.Hour * 30.0F - 90.0F + dtNow.Minute * 0.5F);
			painter.SmoothingMode = SmoothingMode.AntiAlias;
           	drawClockHand( hourColor, new Size(45, 3), 10, true );
           	
           	painter.ResetTransform();
           	painter.TranslateTransform( 65, 65 );
			painter.RotateTransform(dtNow.Second * 6.0F - 90.0F);
			painter.SmoothingMode = SmoothingMode.AntiAlias;
			painter.FillClosedCurve( new SolidBrush(secondColor), new Point[]{ new Point(2,2), new Point(2,-2), new Point(-2,-2), new Point(-2,2)} );
			drawClockHand( secondColor, new Size(60, 2), 10, true );
           	
           	
			GC.Collect();
			
			return result;
	    }
		
		private static void drawClockHand(Color color, Size handSize, int centerAt, bool rectangle ){
			
			if( !rectangle ){
				painter.FillPolygon( new SolidBrush(color),
			                 new Point[]{ new Point(-centerAt, handSize.Height/2), 
			                    			 new Point(-centerAt, -handSize.Height/2), 
			                    			 new Point(handSize.Width,0) } );
			}else{
				painter.FillRectangle(new SolidBrush(color), -centerAt, -handSize.Height/2, handSize.Width, handSize.Height);
			}
		}
		
		
	    #endregion
		
	}
}
